package com.panshi.service.impl;

import com.panshi.mapper.IQuestionMapper;
import com.panshi.service.IQuestionService;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class QuestionService implements IQuestionService {

    @Autowired
    IQuestionMapper questionMapper;

    /*
    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean addQuestion(Question question) {
        try {
            iQuestionMapper.addQuestion(question);
            return true;
        }catch (Exception e){
            return false;
        }
    }
    */

    @Override
    public Pages<Question> inquirePagesQuestion(Pages pages) {
        List<Question> list = questionMapper.inquirePagesQuestion(pages);
        Integer sum = questionMapper.inquireQuestionSum(pages);
        Pages<Question> p = new Pages<>();
        p.setList(list);
        p.setSum(sum);
        return p;
    }

    @Override
    public Integer inquireQuestionSum(Pages pages) {
        Integer sum = questionMapper.inquireQuestionSum(pages);
        return sum;
    }


    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public Question inquireById(int id) {
        Question question = questionMapper.inquireById(id);
        //阅读总数加一
        questionMapper.readSumAnd1(id);
        return question;
    }

    /*@Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean readSumAnd1(int id) {
        try {
            questionMapper.readSumAnd1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }*/

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean collectSumAnd1(int id) {
        try {
            questionMapper.collectSumAnd1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean collectSumSubtract1(int id) {
        try {
            questionMapper.collectSumSubtract1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /*@Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean commentSumAnd1(int id) {
        try {
            questionMapper.commentSumAnd1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }*/

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean commentSumSubtract1(int id) {
        try {
            questionMapper.commentSumSubtract1(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean deleteQuestion(int id) {
        try {
            questionMapper.deleteQuestion(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Pages<Question> inquireMyPagesQuestion(Pages pages) {
        List<Question> list = questionMapper.inquireMyPagesQuestion(pages);
        Integer sum = questionMapper.inquireQuestionSum(pages);
        Pages<Question> p = new Pages<>();
        p.setList(list);
        p.setSum(sum);
        return p;
    }
}
